/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-14     DRZZZ       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include "board.h"
#include "uart_lcd.h"
#include "relay_management.h"

#define LOG_TAG        "UARTLCD"
#define LOG_LVL        LOG_LVL_INFO
#include <ulog.h>

#define THREAD_PRIORITY         6
#define THREAD_TIMESLICE        10
#define LCD_UART       "uart6"



ALIGN(RT_ALIGN_SIZE)
static char LCDuart_stack[1024];
struct rt_thread LCDuart_RT;
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
rt_device_t LCDuart_serial;
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断，调用此回调函数，然后发送接收信号量 */
    rt_sem_release(&rx_sem);
    return RT_EOK;
}

static void LCDuart_entry(void *parameter)
{
    char ch;

    while (1)
    {
        while (rt_device_read(LCDuart_serial, 0, &ch, 1) != 1)
        {
            rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
        }
//        LOG_I("Receive from LCD!");
//        printf("%c",ch);
    }
}
static int LCDuart_thread()
{
    rt_err_t ret = RT_EOK;
    /* 查找系统中的串口设备 */
    LCDuart_serial = rt_device_find(LCD_UART);

    if (!LCDuart_serial)
    {
        rt_kprintf("find %s failed!\n", LCD_UART);
        return RT_ERROR;
    }

    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */

    config.baud_rate = BAUD_RATE_115200;        //修改波特率为 115200
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 128;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位

    rt_device_control(LCDuart_serial, RT_DEVICE_CTRL_CONFIG, &config);

    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(LCDuart_serial, RT_DEVICE_FLAG_INT_RX);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(LCDuart_serial, uart_input);
    /* 创建线程1，名称是trol_RTT，入口是control_entry*/
    rt_thread_init(&LCDuart_RT,
                   "LCD_RT",
                   LCDuart_entry,
                   RT_NULL,
                   &LCDuart_stack[0],
                   sizeof(LCDuart_stack),
                   THREAD_PRIORITY, THREAD_TIMESLICE);
    rt_thread_startup(&LCDuart_RT);
    LOG_I("--UARTLCD init over!");
    return ret;
}
INIT_DEVICE_EXPORT(LCDuart_thread);


void Tolcd_8bite(char address_h,char address_l,char data)
{
    static char frame_1=0x5A,frame_2=0xA5,num=0x05;
    static char write=0x82,data_h=0x00;

    rt_device_write(LCDuart_serial, 0,   &frame_1, 1);
    rt_device_write(LCDuart_serial, 0,   &frame_2, 1);
    rt_device_write(LCDuart_serial, 0,       &num, 1);
    rt_device_write(LCDuart_serial, 0,     &write, 1);
    rt_device_write(LCDuart_serial, 0, &address_h, 1);
    rt_device_write(LCDuart_serial, 0, &address_l, 1);
    rt_device_write(LCDuart_serial, 0,    &data_h, 1);
    rt_device_write(LCDuart_serial, 0,      &data, 1);
}

void Display_refresh()
{
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_1));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_2));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_3));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_4));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_5));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_6));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_7));
   Tolcd_8bite(0x00, 0x10, rt_pin_read(RELAY_PIN_8));

}
